﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.Common;
using System.Data;

namespace Common
{
    public class SqlHelper
    {
        private DbConnection conn;
        public DbConnection GetSqlConnection(string connStr,string type)
        {
            switch (type)
            {
                case "sqlite":conn = new SQLiteConnection(connStr);break;
                default:conn = null;
                    break;
            }
            if (conn != null) conn.Open();
            return conn;
        }
        public int InsertDelUpdate(string cmdText,DbParameter[] paras,string type)
        {
            DbCommand cmd = null;
            switch (type)
            {
                case "sqlite":
                    cmd = new SQLiteCommand(cmdText, (SQLiteConnection)conn);
                    
                    break;
                default:break;
            }
            cmd.Parameters.AddRange(paras);
            return cmd.ExecuteNonQuery();
        }

        public DbDataReader ExecuteReader(string sql,DbParameter[] paras,string type)
        {
            DbCommand cmd = null;
            switch (type)
            {
                case "sqlite":cmd = new SQLiteCommand(sql, (SQLiteConnection)conn);break;
                default:break;
            }
            return cmd.ExecuteReader();
        }
    }
}
